/**
 * Created : July 23, 2012
 *
 * @author pquiring
 */

import java.awt.*;

import javaforce.*;

public class PositionProps extends javax.swing.JPanel {

  /**
   * Creates new form PositionProps
   */
  public PositionProps(Element element) {
    initComponents();
    img = new JFImage();
    img.load(this.getClass().getClassLoader().getResourceAsStream("img200.png"));
    sx1.setValue(element.sx1);
    sy1.setValue(element.sy1);
    sx2.setValue(element.sx2);
    sy2.setValue(element.sy2);
    dx1.setValue(element.dx1);
    dy1.setValue(element.dy1);
    dx2.setValue(element.dx2);
    dy2.setValue(element.dy2);
    drawExample();
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        sy1 = new javax.swing.JSlider();
        sy2 = new javax.swing.JSlider();
        sx1 = new javax.swing.JSlider();
        sx2 = new javax.swing.JSlider();
        jPanel2 = new javax.swing.JPanel();
        dx2 = new javax.swing.JSlider();
        dy1 = new javax.swing.JSlider();
        dy2 = new javax.swing.JSlider();
        dx1 = new javax.swing.JSlider();
        jPanel3 = new javax.swing.JPanel();
        srcImage = new javax.swing.JLabel();
        destImage = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Source"));

        sy1.setOrientation(javax.swing.JSlider.VERTICAL);
        sy1.setValue(0);
        sy1.setInverted(true);
        sy1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sy1StateChanged(evt);
            }
        });

        sy2.setMinimum(5);
        sy2.setOrientation(javax.swing.JSlider.VERTICAL);
        sy2.setValue(100);
        sy2.setInverted(true);
        sy2.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sy2StateChanged(evt);
            }
        });

        sx1.setMaximum(95);
        sx1.setValue(0);
        sx1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sx1StateChanged(evt);
            }
        });

        sx2.setMinimum(5);
        sx2.setSnapToTicks(true);
        sx2.setValue(100);
        sx2.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sx2StateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(sy1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sx2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sx1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sy2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(sy2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sy1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(sx1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(sx2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Destination"));

        dx2.setMinimum(5);
        dx2.setValue(100);
        dx2.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                dx2StateChanged(evt);
            }
        });

        dy1.setMaximum(95);
        dy1.setOrientation(javax.swing.JSlider.VERTICAL);
        dy1.setValue(0);
        dy1.setInverted(true);
        dy1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                dy1StateChanged(evt);
            }
        });

        dy2.setMinimum(5);
        dy2.setOrientation(javax.swing.JSlider.VERTICAL);
        dy2.setValue(100);
        dy2.setInverted(true);
        dy2.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                dy2StateChanged(evt);
            }
        });

        dx1.setMaximum(95);
        dx1.setValue(0);
        dx1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                dx1StateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(dy1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(dx2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dx1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dy2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(dy2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dy1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(dx1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(168, 168, 168)
                        .addComponent(dx2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Example"));

        srcImage.setPreferredSize(new java.awt.Dimension(200, 200));

        destImage.setPreferredSize(new java.awt.Dimension(200, 200));

        jLabel1.setText(">>>>>>>");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(srcImage, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(destImage, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(destImage, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel3Layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(srcImage, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(100, 100, 100)
                        .addComponent(jLabel1)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(41, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

  private void sx1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sx1StateChanged
    int sx_1 = sx1.getValue();
    int sx_2 = sx2.getValue();
    if (sx_1 >= sx_2-5) {
      sx2.setValue(sx_1 + 5);
    }
    drawExample();
  }//GEN-LAST:event_sx1StateChanged

  private void sy1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sy1StateChanged
    int sy_1 = sy1.getValue();
    int sy_2 = sy2.getValue();
    if (sy_1 >= sy_2-5) {
      sy2.setValue(sy_1 + 5);
    }
    drawExample();
  }//GEN-LAST:event_sy1StateChanged

  private void sx2StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sx2StateChanged
    int sx_1 = sx1.getValue();
    int sx_2 = sx2.getValue();
    if (sx_2 <= sx_1+5) {
      sx1.setValue(sx_2 - 5);
    }
    drawExample();
  }//GEN-LAST:event_sx2StateChanged

  private void sy2StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sy2StateChanged
    int sy_1 = sy1.getValue();
    int sy_2 = sy2.getValue();
    if (sy_2 <= sy_1+5) {
      sy1.setValue(sy_2 - 5);
    }
    drawExample();
  }//GEN-LAST:event_sy2StateChanged

  private void dx1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_dx1StateChanged
    int dx_1 = dx1.getValue();
    int dx_2 = dx2.getValue();
    if (dx_1 >= dx_2-5) {
      dx2.setValue(dx_1 + 5);
    }
    drawExample();
  }//GEN-LAST:event_dx1StateChanged

  private void dy1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_dy1StateChanged
    int dy_1 = dy1.getValue();
    int dy_2 = dy2.getValue();
    if (dy_1 >= dy_2-5) {
      dy2.setValue(dy_1 + 5);
    }
    drawExample();
  }//GEN-LAST:event_dy1StateChanged

  private void dx2StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_dx2StateChanged
    int dx_1 = dx1.getValue();
    int dx_2 = dx2.getValue();
    if (dx_2 <= dx_1+5) {
      dx1.setValue(dx_2 - 5);
    }
    drawExample();
  }//GEN-LAST:event_dx2StateChanged

  private void dy2StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_dy2StateChanged
    int dy_1 = dy1.getValue();
    int dy_2 = dy2.getValue();
    if (dy_2 <= dy_1+5) {
      dy1.setValue(dy_2 - 5);
    }
    drawExample();
  }//GEN-LAST:event_dy2StateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel destImage;
    private javax.swing.JSlider dx1;
    private javax.swing.JSlider dx2;
    private javax.swing.JSlider dy1;
    private javax.swing.JSlider dy2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JLabel srcImage;
    private javax.swing.JSlider sx1;
    private javax.swing.JSlider sx2;
    private javax.swing.JSlider sy1;
    private javax.swing.JSlider sy2;
    // End of variables declaration//GEN-END:variables

  private JFImage img;

  private void drawExample() {
    int sx_1 = sx1.getValue();
    int sy_1 = sy1.getValue();
    int sx_2 = sx2.getValue();
    int sy_2 = sy2.getValue();
    int dx_1 = dx1.getValue();
    int dy_1 = dy1.getValue();
    int dx_2 = dx2.getValue();
    int dy_2 = dy2.getValue();
    JFImage src = new JFImage();
    src.setImageSize(200, 200);
    src.putJFImage(img, 0, 0);
    src.getGraphics().setColor(Color.BLACK);
    src.getGraphics().drawRect(0,0,199,199);
    src.getGraphics().drawRect(sx_1*2, sy_1*2, (sx_2 - sx_1)*2-1, (sy_2 - sy_1)*2-1);
    srcImage.setIcon(src);
    JFImage dest = new JFImage();
    dest.setImageSize(200, 200);
    dest.getGraphics().drawImage(img.getImage(), dx_1*2, dy_1*2, dx_2*2, dy_2*2, sx_1*2, sy_1*2, sx_2*2, sy_2*2, null);
    dest.getGraphics().setColor(Color.BLACK);
    dest.getGraphics().drawRect(0,0,199,199);
    dest.getGraphics().drawRect(dx_1*2, dy_1*2, (dx_2 - dx_1)*2-1, (dy_2 - dy_1)*2-1);
    destImage.setIcon(dest);
  }

  public void save(Element e) {
    e.sx1 = sx1.getValue();
    e.sy1 = sy1.getValue();
    e.sx2 = sx2.getValue();
    e.sy2 = sy2.getValue();
    e.dx1 = dx1.getValue();
    e.dy1 = dy1.getValue();
    e.dx2 = dx2.getValue();
    e.dy2 = dy2.getValue();
  }

}
